/* 
Scenic Graphics Library
Copyright (C) 2007 Jouni Tulkki

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USApackage scenic;
*/

package scenic;

import java.awt.Rectangle;
import java.awt.geom.AffineTransform;

import scenic.jni.Renderer;

/**
 * This is the base class for shapes. Shapes are scene nodes that 
 * specify an area. The area of the shape is drawn with a color. 
 * The default color is white (red=1, green=1, blue=1, alpha=1).
 */
public abstract class SceneShape extends SceneLeaf {
	private static final Color white = new Color(1.0f, 1.0f, 1.0f, 1.0f);
	
	private Color color = white;
	
	public SceneShape() {
		super();
	}

	/**
	 * Gets the shape color.
	 */
	public Color getColor() {
		return color;
	}
	
	/**
	 * Sets the shape color.
	 */
	public void setColor(Color color) {
		this.color = color;
		changed();
	}
		
	void draw(DrawContext context, AffineTransform transform, Rectangle visibleArea) {
		int surfaceType = getSurfaceType(getDrawType(context, transform, color));
		
		if(surfaceType == Renderer.SURFACE_TYPE_COLOR || 
				(surfaceType == context.surfaceType && color.isWhite())) {
			draw(context, transform, color);
		} else {
			Rectangle bounds = getBounds(context, transform);
			
			if(bounds.width <= 0 || bounds.height <= 0)
				return;
			if(context.renderer.beginSurface(context.context, addSafetyMargin(bounds), surfaceType) == 0)
				return;
			draw(context, transform, white);
			context.renderer.color(context.context, color);
			context.renderer.drawSurface(context.context);
		}
	}
	
	abstract void draw(DrawContext context, AffineTransform transform, Color color);

	void prepareDraw(DrawContext context, AffineTransform transform) {
	}
	
	int getDrawType(DrawContext context, AffineTransform transform) {
		return getDrawType(context, transform, color);
	}
	
	int getDrawType(DrawContext context, AffineTransform transform, Color color) {
		return DRAW_SIMPLE;
	}
	
	abstract Rectangle getBounds(DrawContext context, AffineTransform transform);
}
